(function(){
    var globalConfig = {
		/* 
		* 建议最高配置三级级联菜单,否则会破坏页面布局
		* name: String, 菜单名称
		* class: String, 菜单类名, "one", "one two"
		* attributes: {}, 属性列表, {id: "index","data-id": "data-id"}
		* nodes: 子菜单，结构与根菜单一致
		* visibility： Boolean, 是否显示
		*/
        nodes: [
        	{
        		name: "首页",
        		class: "index-lv1",
				attributes: {
					id: "index",
					"data-id": "data-id"
				},
				nodes: [
					{
						name: "二级1",
						class: "index-lv2"
					},
					{
						name: "二级2",
						class: "index-lv2",
						nodes: [
							{
								name: "三级",
								class: "index-lv3"
							}
						]
					}
				]
        	},
        	{
        		name: "业务中心"
        	},
        	{
        		name: "用户中心"
        	},
        	{
        		name: "设置中心",
        		visibility: false
        	},
        	{
        		name: "关于我们"
        	}
        ]
    };
	var sidebar = document.querySelector(".sidebar");
    (function(){
    	var nodesList = ['<div class="now-tag"></div>'];
		var parseNodes = function(args){
			var nodes = [];
			args.map(function(v){
				var node = '<div class="node';
				if (v.class) node += (' ' + v.class);
				node += '"';
				if (v.visibility === false) node += (' style="display:none;"');
				for (var iv in v.attributes) {
					node += (' ' + iv + '="' + v.attributes[iv] + '"');
				}
				node += '>';
				node += (v.name || '');
				if (v.nodes) node += (parseNodes(v.nodes) || '');
				node += '</div>';
				nodes.push(node);
			});
			return nodes.join('');
		}
		nodesList.push(parseNodes(globalConfig.nodes));
    	sidebar.innerHTML = nodesList.join('');
    })();
	(function(){
		sidebar.addEventListener('click', function(e){
			var _this = e.target;
			if (!(_this.classList.contains('now'))) {
				var now = document.querySelector('.now');
				if (now) now.classList.remove("now");
				_this.classList.add('now');
			}
			_this.classList.add('open');
			$(_this).siblings().removeClass('open');
			var tag = document.querySelector('.now-tag');
			var top = _this.offsetTop + 'px';
			tag.style.top = top;
		}, false);
	})();
})();